// mircoApp/userAccount/windAccount/windAccount.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [skinBehavior],
  data: {
    windForm:{
      bank_card_no:'',
      bank_province_id:'',
      bank_city_id:'',
      bank_province_name:'',
      bank_city_name:'',
      id_front_pic:'',
      id_behind_pic:'',
      bank_card_before_pic:'',
      bank_card_behind_pic:''
    },
    fileList:[],
    fileList2:[],
    fileList3:[],
    fileList4:[],
    uploadUrl: "upload.uploadPic", //上传图片
    addressName:'',
    addressShow:false,
    //街道
    selectImgIndex: "",
    stopButton: false,
    withdrawInfo:{},
    iPnoneBottomBol:false, //是否是苹果手机
    succeedRadio:'1',
    windStatus:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.withdrawData();
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  modelInput(event) {
    const item = event.currentTarget.dataset.item,
          itemValue = event.detail.value;
    this.setData({
      [item]:itemValue
    })
  },
  userAccountTo() {
    wx.navigateBack({
      delta: 1
    });
  },
  onAddress() {
    this.setData({
      addressShow:true
    })
  },
  getAddress(event) {
    let seleData = event.detail;
    let retCity = seleData[0].name + ' ' + seleData[1].name;
    this.setData({
      addressName: retCity,
      'windForm.bank_province_name':seleData[0].name,
      'windForm.bank_city_name':seleData[1].name,
      'windForm.bank_province_id':seleData[0].id,
      'windForm.bank_city_id':seleData[1].id,
      addressShow:false,
    })
  },
  windSetOn() {
    this.setData({
      windStatus:2
    })
  },
  withdrawData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.integration-pay-share.frontend.withdraw.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          if(!Array.isArray(data)) {
            let settleInfo = data.has_one_active_settle_info;
            if(settleInfo) {
              this.setData({
                withdrawInfo:data,
                'windForm.bank_card_no':settleInfo.bank_card_no,
                'windForm.bank_province_id':settleInfo.bank_province_id,
                'windForm.bank_city_id':settleInfo.bank_city_id,
                'windForm.bank_province_name':settleInfo.bank_province_name,
                'windForm.bank_city_name':settleInfo.bank_city_name,
                'windForm.id_front_pic':settleInfo.id_front_pic,
                'windForm.id_behind_pic':settleInfo.id_behind_pic,
                'windForm.bank_card_before_pic':settleInfo.bank_card_before_pic,
                'windForm.bank_card_behind_pic':settleInfo.bank_card_behind_pic,
                addressName:settleInfo.bank_province_name + settleInfo.bank_city_name,
                windStatus:1
              })
            }
            
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  onRead(event) { //图片上传功能
    let itemName = event.currentTarget.dataset.itemname;
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFiles;
        console.log(tempFilePaths[0]);
        let photourl = tempFilePaths[0].tempFilePath;
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: 'file',
          success(resdata) {
            let res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                [itemName]: res.data.img_url
              });
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          }
        });
      }
    });
  },
  windSubmit() {
    if(app._isTextEmpty(this.data.windForm.bank_card_no)) {
      wx.showToast({
        icon: 'none',
        title: '请输入结算账号',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.addressName)) {
      wx.showToast({
        icon: 'none',
        title: '请选择银行所在省市',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.addressName)) {
      wx.showToast({
        icon: 'none',
        title: '请选择银行所在省市',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.windForm.id_front_pic)) {
      wx.showToast({
        icon: 'none',
        title: '请上传身份证正面照片',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.windForm.id_behind_pic)) {
      wx.showToast({
        icon: 'none',
        title: '请上传身份证反面照片',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.windForm.bank_card_before_pic)) {
      wx.showToast({
        icon: 'none',
        title: '请上传结算卡正面照片',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.windForm.bank_card_behind_pic)) {
      wx.showToast({
        icon: 'none',
        title: '请上传结算卡反面照片',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    let urlStr = app.getNetAddresss("plugin.integration-pay-share.frontend.withdraw.set-settle-info");
    app._postNetWork({
      url: urlStr,
      data:{form:this.data.windForm},
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
          wx.hideLoading();
          this.withdrawData();
          setTimeout(() => {
            wx.redirectTo({
              url: '/packageG/member_v2/member_v2',
            })
          },1000);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})